﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class admin_vote_Upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpPostedFile file = Request.Files["Filedata"];

        if (file != null)
        {
            //获取上传目录路径
            string uploadPath = HttpContext.Current.Server.MapPath(string.Format(@"~/proImg/{0}/", DateTime.Now.ToString("yyyyMMdd")));
            if (!System.IO.Directory.Exists(uploadPath))
            {
                System.IO.Directory.CreateDirectory(uploadPath);
            }

            //获取缩略图路径
            string thumbsPath = HttpContext.Current.Server.MapPath(string.Format(@"~/thumbs/{0}/", DateTime.Now.ToString("yyyyMMdd")));
            if (!System.IO.Directory.Exists(thumbsPath))
            {
                System.IO.Directory.CreateDirectory(thumbsPath);
            }

            //文件名
            string strFileName = string.Format("{0}{1}{2}.jpg", Convert.ToInt32(Request["ApplyFeeID"]), DateTime.Now.ToString("yyyyMMddhhmmssfff"), new Random().Next(999999999));

            //物理磁盘绝对路径
            string strFullPath = string.Format("{0}{1}", uploadPath, strFileName);

            //转换为Url
            string strUrl = UrlConvertor(strFullPath);

            //保存原图
            file.SaveAs(strFullPath);



            SendSmallImage(uploadPath + strFileName, thumbsPath + strFileName, 90, 90);

            //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
            //Response.Write("1");
            Response.Write(file.FileName.ToLower().Replace(".jpg", "").Replace(".gif", "").Replace(".png", "") + "," + strUrl);
            Response.End();
        }
        else
        {
            Response.Write("0");
        }
    }

    /// <summary>
    /// 本地路径转换成URL相对路径
    /// </summary>
    /// <param name="strImagesUrl"></param>
    /// <returns></returns>
    private string UrlConvertor(string strImagesUrl)
    {
        string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
        string imagesurl2 = strImagesUrl.Replace(tmpRootDir, ""); //转换成相对路径
        imagesurl2 = imagesurl2.Replace(@"\", @"/");
        imagesurl2 = string.Format("/{0}", imagesurl2);
        return imagesurl2;
    }


    private static Size NewSize(int maxWidth, int maxHeight, int width, int height)
    {
        double w = 0.0;
        double h = 0.0;
        double sw = Convert.ToDouble(width);
        double sh = Convert.ToDouble(height); double mw = Convert.ToDouble(maxWidth);
        double mh = Convert.ToDouble(maxHeight); if (sw < mw && sh < mh)
        {
            w = sw;
            h = sh;
        }
        else if ((sw / sh) > (mw / mh))
        {
            w = maxWidth;
            h = (w * sh) / sw;
        }
        else
        {
            h = maxHeight;
            w = (h * sw) / sh;
        }
        return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
    }

    public static void SendSmallImage(string fileName, string newFile, int maxHeight, int maxWidth)
    {
        System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
        System.Drawing.Imaging.ImageFormat
        thisFormat = img.RawFormat;
        Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
        Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
        Graphics g = Graphics.FromImage(outBmp);
        // 设置画布的描绘质量
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
        0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
        g.Dispose();
        // 以下代码为保存图片时,设置压缩质量
        EncoderParameters encoderParams = new EncoderParameters();
        long[] quality = new long[1];
        quality[0] = 100;
        EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        encoderParams.Param[0] = encoderParam;
        //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
        ImageCodecInfo jpegICI = null;
        for (int x = 0;
        x < arrayICI.Length;
        x++)
        {
            if (arrayICI[x].FormatDescription.Equals("JPEG"))
            {
                jpegICI = arrayICI[x];
                //设置JPEG编码
                break;
            }
        }
        if (jpegICI != null)
        {
            outBmp.Save(newFile, jpegICI, encoderParams);
        }
        else
        {
            outBmp.Save(newFile,
            thisFormat);
        }
        img.Dispose();
        outBmp.Dispose();
    }
}